package shizhan.proxy;

import shizhan.dao.CalculatorDao;

public class CalculatorStaticProxy implements CalculatorDao {
    /**
     * 静态代理类:为所有计算器实现类添加日志
     */
    private final CalculatorDao target;

    // 记录运算类型(用于日志显示,如"加法")
    private final String operatorName;

    // 构造器注入目标对象和运算名
    public CalculatorStaticProxy(CalculatorDao target, String operatorName) {
        this.target = target;
        this.operatorName = operatorName;
    }



    @Override
    public double calculator(double a, double b) {
        // 日志增强:前置(记录方法调用,参数)
        System.out.printf("【日志】开始执行%s运算，参数:num1=%.2f,name2=%.2f%n",operatorName,a,b);
        double result;
        try {
            // 调用目标业务方法(核心运算逻辑)
            result = target.calculator(a,b);
            // 日志增强:后置(记录成功结果)
            System.out.printf("【日志】%s运算执行成功,结果: %.2f%n",operatorName,result);
        } catch (Exception e) {
            System.out.printf("【日志】%s运算执行失败,异常: %s%n",operatorName,e.getMessage());
            throw e;
        } finally {
        }
        return result;
    }

//    public void before(){
//
//    }
}
